package com.sos.core.game.requests 
{
	import com.sos.core.Position;
	import com.sos.core.game.Game;
	import com.sos.core.game.GameAction;
	import com.sos.core.game.GameActionCard;
	import com.sos.core.game.GameActionRole;
	import com.sos.core.game.GameActionTarget;
	import com.sos.core.game.GameTarget;
	import com.sos.logging.Logger;
	
	import flash.utils.Dictionary;

	public class GameRequestPlayParams extends GameRequestParams
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON properties names
		protected static const		JSON_PROPERTY_ACTION:String             	= "action";
		protected static const		JSON_PROPERTY_ACTION_TARGET:String          = "actionTarget";
		protected static const		JSON_PROPERTY_PLAYED_CARDS:String			= "cardsPlayed";
		protected static const		JSON_PROPERTY_PLAYED_CARD_ID:String         = "id";
		protected static const		JSON_PROPERTY_PLAYED_CARD_PARAMS:String     = "params";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _role:GameActionRole;
		private var _action:GameAction;
		private var _target:Position;
		private var _cards:Vector.<GameActionCard>; // <GameActionCard, GameActionTarget>
		private var _targets:Vector.<GameActionTarget>
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameRequestPlayParams()
		{
			super();
			// Init objects
			_cards = new Vector.<GameActionCard>();
			_targets = new Vector.<GameActionTarget>();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------

		public function get action():GameAction
		{
			return _action;
		}
		
		public function set action(value:GameAction):void
		{
			_action = value;
		}

		
		public function get role():GameActionRole
		{
			return _role;
		}
		
		public function set role(value:GameActionRole):void
		{
			_role = value;
		}
		
		public function get target():Position
		{
			return _target;
		}
		
		public function set target(value:Position):void
		{
			_target = value;
		}
		
		public function get cards():Vector.<GameActionCard>
		{
			return _cards;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function createPlayedCardJson(card:GameActionCard, target:GameActionTarget):Object
		{
			// Target json
			var jsonTarget:Object = new Object();
			if (target.hasParams)
			{
				for (var name:String in target.params)
				{
					jsonTarget[name] = target.getParamValue(name);
				}
			}
			// Card JSON
			var jsonCard:Object = new Object();
			jsonCard[JSON_PROPERTY_PLAYED_CARD_ID] = card.instanceId.toString();
			if (target.hasParams) jsonCard[JSON_PROPERTY_PLAYED_CARD_PARAMS] = jsonTarget; 
			// Return JSON card target
			return jsonCard;
		}
		
		override protected function createActionData():Object
		{
			var json:Object = new Object();
			//var card:GameActionCard = null;
			var cards:Array = new Array();
			var target:GameActionTarget;
			// Encode all played cards to JSON
			for (var ac:uint = 0; ac < _cards.length; ac++) {
				
				Logger.debug("-----------------> GameRequestPlayParams > handling card " + _cards[ac].name + " with target type " + _targets[ac].type);
				cards.push(createPlayedCardJson(_cards[ac], _targets[ac]));
			}
			// PLAY phase parameters are provided only if the player is ATTACKING	
			if (_role == GameActionRole.ATTACKER)
			{
				json[JSON_PROPERTY_ACTION] = _action.name;
				// Target position i used only fo PASS or DRIBLE
				if (_action == GameAction.PASS || _action == GameAction.DRIBBLE)
				{
					json[JSON_PROPERTY_ACTION_TARGET] = _target.sign;
				}
			}
			// Put the cards to play
			json[JSON_PROPERTY_PLAYED_CARDS] = cards;
			// Encoded
			return json;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------

		/**
		 * 
		 * @param card
		 * @param target
		 * 
		 */		
		public function addCard(card:GameActionCard, target:GameActionTarget):void
		{
			// LT_TODO: validare la selezione della carta in qualche modo
			// se vengono fatte bene le selezioni non dovrebbe essere necessario
			// visto che il Gameengine non fornirà la lista delle carte non giocabili a priori
			_cards.push(card);
			_targets.push(target);
		}
		
		/**
		 * 
		 * @param card
		 */		
		public function removeCard(card:GameActionCard):void
		{
			//delete _cards[card];
		}
		
		/**
		 * 
		 */		
		public function clearCards():void
		{
			_cards.splice(0, _cards.length);
			_targets.splice(0, _targets.length);
		}
		
		
	}
}